package org.elec.mac.ws;

import lombok.extern.slf4j.Slf4j;
import org.elec.mac.ehcache.CacheConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * Created by Bean on 2017/10/12.
 */
@Slf4j
@EnableWebSocket
@Configuration
public class WebSocketServer implements WebSocketConfigurer {

    @Autowired
    private CacheConfiguration cacheConfiguration;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        log.info("registerWebSocketHandlers");
        WebSocketHandler socketHandler = new WebSocketHandler();
        socketHandler.setCacheConfiguration(cacheConfiguration);
        registry.addHandler(socketHandler, "/ws").addInterceptors(new WebSocketInterceptor()).setAllowedOrigins("*");
    }
}
